package com.artconcurrency.chapter4;

/**
 * @ClassName com.artconcurrency.chapter4
 * Description: <类功能描述>. <br>
 * <p>
"Blocked thread2" #14 prio=5 os_prio=31 tid=0x00007fd2bf069000 nid=0x5f03 waiting for monitor entry [0x0000700004224000]
    java.lang.Thread.State: BLOCKED (on object monitor)
    at com.artconcurrency.chapter4.ThreadState$Blocked.run(ThreadState.java:61)
    - waiting to lock <0x000000076adb21c8> (a java.lang.Class for com.artconcurrency.chapter4.ThreadState$Blocked)
    at java.lang.Thread.run(Thread.java:748)

"Blocked thread1" #13 prio=5 os_prio=31 tid=0x00007fd2bc002800 nid=0x5d03 waiting on condition [0x0000700004121000]
    java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:340)
    at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
    at com.artconcurrency.chapter4.SleepUtils.second(SleepUtils.java:18)
    at com.artconcurrency.chapter4.ThreadState$Blocked.run(ThreadState.java:61)
    - locked <0x000000076adb21c8> (a java.lang.Class for com.artconcurrency.chapter4.ThreadState$Blocked)
    at java.lang.Thread.run(Thread.java:748)

"Waiting thread" #12 prio=5 os_prio=31 tid=0x00007fd2bf888800 nid=0x5b03 in Object.wait() [0x000070000401e000]
    java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076adadea8> (a java.lang.Class for com.artconcurrency.chapter4.ThreadState$Waiting)
    at java.lang.Object.wait(Object.java:502)
    at com.artconcurrency.chapter4.ThreadState$Waiting.run(ThreadState.java:44)
    - locked <0x000000076adadea8> (a java.lang.Class for com.artconcurrency.chapter4.ThreadState$Waiting)
    at java.lang.Thread.run(Thread.java:748)

"Time waiting thread" #11 prio=5 os_prio=31 tid=0x00007fd2bf802800 nid=0x5903 waiting on condition [0x0000700003f1b000]
    java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:340)
    at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
    at com.artconcurrency.chapter4.SleepUtils.second(SleepUtils.java:18)
    at com.artconcurrency.chapter4.ThreadState$TimeWaiting.run(ThreadState.java:29)
    at java.lang.Thread.run(Thread.java:748)

 * </p>
 * @Author tree
 * @Date 2019-05-01 18:18
 * @Version 1.0
 */
public class ThreadState {
    public static void main(String[] args) {
        new Thread(new TimeWaiting(),"Time waiting thread").start();
        new Thread(new Waiting(),"Waiting thread").start();
        //使用2个Block线程， 一个获取锁成功，一个被阻塞
        new Thread(new Blocked(),"Blocked thread1（Time waiting）").start();
        new Thread(new Blocked(),"Blocked thread2").start();
    }

    /**
     * 该线程不断进行睡眠
     */
    static class TimeWaiting implements Runnable{
        @Override
        public void run() {
            while (true){
                SleepUtils.second(100);
            }
        }
    }

    /**
     * 该线程在Waiting.class实例上等待
     */
    static class Waiting implements Runnable{

        @Override
        public void run() {
            while (true){
                synchronized (Waiting.class){
                    try {
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 该线程在Blocked.class实例上加锁后，不会释放该锁
     */
    static class Blocked implements Runnable{
        @Override
        public void run() {
            synchronized (Blocked.class){
                while (true){
                    SleepUtils.second(100);
                }
            }
        }
    }

}
